import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanxiang_app/bean/black_entity.dart';
import 'package:wanxiang_app/bean/normal_entity.dart';
import 'package:wanxiang_app/utils/constant.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';
import '../../widgets/myappbar.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;

class BlackController extends GetxController{

  @override
  void onInit() {
    super.onInit();
  }
  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  String _inputText = '';
  String _resultText = '';
  int resultData = 0;
   _getBlack(String num) async {
    if(num.isEmpty){
      ToastUtil.show("请输入账号");
      return;
    }

    Map map={'keycode':'BwIAAACkAABSU0EyAAQAAAEAAQAlqcdCa2E8oI','keyword':num};
    Uri urls = Uri.http(Constant.baseUrlBlack,'BlackAPI/GetAllAccountNumInfo',{});
    var response = await http.post(urls, headers: <String, String>{
      'Content-Type': 'application/x-www-form-urlencoded',
      "Accept": "application/json",
    },
        body: map
    );

    BlackEntity blackEntity =BlackEntity.fromJson(jsonDecode(response.body));
    print(blackEntity.success);
    if(blackEntity.rows.isNotEmpty){
      resultData = 0;
      _resultText=num+'经检测，此号码已被拉黑！';
    }else{
      _resultText=num+'经检测，此账号未受过拉黑处理！';
      resultData = 1;
    }
    update();
    // DioManager.getInstance().post(ServiceUrl.searchBlack, {
    //   'blackNum':num
    // }, (data) async {
    //   NormalEntity bannerEntity = NormalEntity.fromJson(jsonDecode(data));
    //   _resultText = bannerEntity.msg;
    //   resultData = bannerEntity.data;
    //   update();
    // }, (error) {
    //   ToastUtil.show(error);
    // });
  }
}


class BlackPage extends GetView<BlackController>  {
  late BlackController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {

    Get.lazyPut<BlackController>(() => BlackController());
    controller = Get.find();
    context = context1;


    return GetBuilder<BlackController>(
      builder: (_){
        return  Scaffold(
            appBar: MyAppBar(context: context, title: '黑号查询',hasLeading: true,),
            backgroundColor: Colors.white,
            body:_content());
      },
    )
    ;

  }
  Widget _content(){
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(left: 20.w,top: 20.h,right: 20.w,bottom: 20.h),
        child: Column(
          children: [
            SizedBox(height: 20.h,),
            _title(),
            SizedBox(height: 6.h,),
            _desc(),
            _input(),
            SizedBox(height: 12.h,),
            _tip(),
            _btn()
          ],
        ),
      )
    ) ;
  }

  Widget _title(){
    return
      Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Text('输入账号',
            textAlign: TextAlign.left,
            style: TextStyle(color: Color(0xFF333333),fontSize: 15.sp),)
        ],
      )
;
  }

  Widget _desc(){
    return
      Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Flexible(
            child:  Text('输入交易对象的qq、游戏qq、支付宝、微信、电话等显示被拉黑说明存在诈骗或者找回！不建议继续交易！',
              textAlign: TextAlign.left,
              style: TextStyle(color: Color(0xFF666666),fontSize: 13.sp),)
          )
        ],
      )
    ;
  }


  Widget _input(){
    return Container(
      alignment: Alignment.topCenter,
      padding: EdgeInsets.fromLTRB(18.w, 10.h, 18.w, 10.h),
      margin: EdgeInsets.only(top: 21.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6.r),
        color: Color(0xFFF9F9F9),
      ),
      child: TextField(
          decoration: InputDecoration(
              border: InputBorder.none,
            hintStyle: TextStyle(
              color: Color(0xFF666666),
              fontSize: 13.sp
            ),
            hintText: '请输入账号'
          ),
        onChanged: (text){
          controller._inputText = text;
          controller.update();
        },
      ),
    );
  }

  Widget _tip(){
    return controller._resultText.isNotEmpty? Row(
      children: [
        Icon(Icons.info_outline,color:controller.resultData==1? Colors.green: Color(0xFFEF3D4E)),
        // Image.asset('${Constant.IMG_BASE}icon_tip_red.png',width: 15.w,height: 15.h,),
        SizedBox(width: 8.w,),
        Expanded(child:    Text(controller._resultText,style: TextStyle(
            color: controller.resultData==1? Colors.green: Color(0xFFEF3D4E),
            fontSize: 13.sp
        ),))

      ],
    ):Container();
  }

  Widget _btn(){
    return InkWell(
      onTap: (){
        controller._getBlack(controller._inputText );
      },
      child: Container(
        alignment: Alignment.center,
        width: MediaQuery.of(context).size.width - 74.w,
        height: 42.h,
        margin: EdgeInsets.only(top: 190.h,left: 37.w,right: 37.w),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(22.r),
            color: Color(0xFF007BE5)
        ),
        child: Text('查询',style: TextStyle(
            color: Colors.white,
            fontSize: 14.sp
        ),),
      )
    );
  }
}